/*
* HeaderController.java
* Created on  2015/7/27 15:10
* 版本       修改时间          作者      修改内容
* V1.0.1    2015/7/27       longshihui    初始版本
*
*/

package com.infitecs.eyas.tools;

import com.infitecs.eyas.expression.service.ToolExpressionService;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.tool.Header;
import com.infitecs.krill.ajax.response.AjaxResponse;
import com.infitecs.krill.ajax.response.ReturnState;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Controller
@RequestMapping("/tools/header")
public class HeaderController {
    @Autowired
    private ToolExpressionService toolExpressionService;

    @Autowired
    private SessionService sessionService;
    @RequestMapping("")
    public String getPage(){
        return "tools/header";
    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public void update(Header header){
        toolExpressionService.updateToolsParam(header);
    }

    @RequestMapping(value = "/calE15", method = RequestMethod.GET)
    @ResponseBody
    public Double calE15() {
        return toolExpressionService.calculate("TE15");
    }

    @RequestMapping(value = "/clean", method = RequestMethod.POST)
    @ResponseBody
    public void clean() {
        toolExpressionService.clearMap();
        toolExpressionService.clearResultMap();
    }

    @RequestMapping(value = "/calAll",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Double> calAll(){
        Map<String,Double> re = new HashMap<>();
        if (!toolExpressionService.hasParamValue("E15")) {
            re.put("E15", toolExpressionService.calculate("TE15"));
        }
        re.put("E19",toolExpressionService.calculate("TE19"));
        re.put("E21",toolExpressionService.calculate("TE21"));
        re.put("E240", toolExpressionService.calculate("TE240"));
        re.put("E240", toolExpressionService.calculate("TE240"));
        re.put("E248", toolExpressionService.calculate("TE248"));
        re.put("E249", toolExpressionService.calculate("TE249"));
        toolExpressionService.getResult().put("p", re.get("E19"));
        toolExpressionService.getResult().put("z", re.get("E248"));
        toolExpressionService.getResult().put("lost",re.get("E19") * re.get("E248"));
        return re;
    }

    @RequestMapping(value = "headerCal", method = RequestMethod.POST)
      @ResponseBody
      public AjaxResponse headerCal(@RequestBody Map<String, Double> paramMap) {
          try {
              return  toolExpressionService.headerCal(paramMap);
          } catch (Exception e) {
              e.printStackTrace();
              if (e instanceof ValidateErrorException) {
                  return new AjaxResponse(ReturnState.WARNING, e.getMessage());
              }
              return new AjaxResponse(ReturnState.ERROR, "请检查填写数据是否有误，操作失败");
          }
      }
}
